﻿using Microsoft.Extensions.DependencyInjection;
using Modules.Device;
using Modules.Device.Entitys;
using Modules.Project.Entitys;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.Project.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ProjectCheckinDeviceRepository : Repository<CheckinDevice, DefaultDbContext>
    {
        public ProjectCheckinDeviceRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
        {
        }

        /// <summary>
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<CheckinDevice> ProjectCheckinDevices(CheckInDeviceQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("checkin_device");
            if (!string.IsNullOrEmpty(query.Keywords))
                sql.Where("(name Like @0 or code Like @0)", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
            sql.Where("project_id=@0", query.ProjectId);
            if (query.Type.HasValue)
                sql.Where("type=@0", query.Type);
            if (query.InOut.HasValue)
                sql.Where("in_out=@0", query.InOut);
            sql.OrderBy("create_time DESC");
            return GetPagingEntities(pageSize, pageIndex, sql);
        }

        public bool ExistsByPrjIdAndcode(CheckinDevice projectCheckinDevice)
        {
            if (string.IsNullOrEmpty(projectCheckinDevice.Code))
                return false;
            return DbContext.Exists<CheckinDevice>("project_id=@0 AND code=@1", projectCheckinDevice.ProjectId, projectCheckinDevice.Code);
        }

        public bool ExistsByPrjIdAndcode(CheckinDevice projectCheckinDevice, int id)
        {
            if (string.IsNullOrEmpty(projectCheckinDevice.Code))
                return false;
            return DbContext.Exists<CheckinDevice>("project_id=@0 AND code=@1 AND id!=@2", projectCheckinDevice.ProjectId, projectCheckinDevice.Code, id);
        }

        public CheckinDevice? GetProjectCheckinDeviceByCode(string code)
        {
            Sql sql = Sql.Builder.Select("id").From("checkin_device").Where("code=@0", code);
            var id = DbContext.FirstOrDefault<int>(sql);
            if (id == 0)
                return null;
            return Get(id);
        }

        public IEnumerable<CheckinDevice> GetProjectCheckinDevicesByProjectId(int projectId)
        {
            int areaVersion = EntityCacheService.GetAreaVersion("ProjectId", projectId);
            return GetTopEntities(1000, CachingExpirationType.ObjectCollection, () =>
            {
                return $"ProjectCheckinDevicesByProjectId:{projectId}:{areaVersion}";
            }, () =>
            {
                Sql sql = Sql.Builder
                .Select("id")
                .From("checkin_device")
                .Where("project_id=@0", projectId);
                return sql;
            });
        }

        public IEnumerable<CheckinDevice> GetProjectCheckinDevicesByProjectIds(IEnumerable<int> projectIds)
        {
            if (projectIds == null || projectIds.Count() == 0)
                return Enumerable.Empty<CheckinDevice>();
            Sql sql = Sql.Builder
                   .Select("id")
                   .From("checkin_device")
                   .Where("project_id in (@0)", projectIds);
            return PopulateEntitiesByEntityIds(DbContext.Fetch<int>(sql));
        }
    }
}